package com.apache.flow.web;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.StringWriter;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.http.HttpServletResponse;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.jbpm.api.ExecutionService;
import org.jbpm.api.JbpmException;
import org.jbpm.api.ProcessDefinition;
import org.jbpm.api.ProcessDefinitionQuery;
import org.jbpm.api.ProcessEngine;
import org.jbpm.api.RepositoryService;
import org.jbpm.api.TaskService;
import org.jbpm.pvm.internal.repository.RepositoryServiceImpl;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

import com.apache.api.vo.ResultEntity;
import com.apache.client.UctCoreClient;
import com.apache.common.DateUtil;
import com.apache.common.FlowBaseAction;
import com.apache.common.FlowClientUtil;
import com.apache.common.OracleConnection;
import com.apache.core.orm.Page;
import com.apache.core.utils.web.struts2.Struts2Utils;
import com.apache.flow.core.entity.FlowDeploy;
import com.apache.flow.core.entity.FlowTaskUrl;
import com.apache.flow.core.manager.FlowDeployManager;
import com.apache.flow.core.manager.FlowInitManager;
import com.apache.flow.core.manager.FlowSysManager;
import com.apache.flow.core.manager.FlowTaskUrlManager;
import com.apache.flow.vo.FlowDeployvo;
import com.apache.flow.vo.ProcessDefinitionVo;
import com.apache.passport.common.PassportHelper;
import com.apache.tools.NumberUtils;
import com.apache.tools.StrUtil;
import com.apache.uct.common.LoginUser;
import com.apache.uct.common.PBOSSOTools;
import com.apache.uct.common.entity.Act;
import com.apache.uct.common.entity.Org;
import com.apache.uct.common.entity.Role;
import com.apache.uct.common.entity.Sys;
import com.opensymphony.xwork2.ActionContext;

@Namespace("/flow/core")
@Results({ @Result(name = "init", location = "flow-deploy.jsp"),
		@Result(name = "design", location = "flow-design.jsp"),
		@Result(name = "toupload", location = "flow-upload.jsp"), @Result(name = "pic", location = "flow-pic.jsp"),
		@Result(name = "toupdate", location = "flow-update.jsp") })
public class FlowDeployAction extends FlowBaseAction {
	private static final long serialVersionUID = 1L;
	private Logger logger = LoggerFactory.getLogger(FlowDeployAction.class);

	@Autowired
	private ProcessEngine processEngine;
	@Autowired
	private FlowInitManager flowInitManager;
	@Autowired
	private FlowTaskUrlManager flowTaskUrlManager;
	@Autowired
	private FlowSysManager flowSysManager;
	@Autowired
	private FlowDeployManager flowDeployManager;

	/**
	 * 流程实例id
	 */
	private String processInsId;

	/**
	 * 流程节点信息
	 */
	private String jsonnodes;

	/**
	 * 上传发布流程，必须为zip
	 */
	private File upload;

	/**
	 * 流程定义文件名称
	 */
	private String uploadFileName;

	/**
	 * 流程图片名称
	 */
	private String imgUploadFileName;

	/**
	 * 文件类型
	 */
	private String uploadContentType;

	/**流程发布id*/
	private String deploymentId;
	/**
	 * 流程定义id
	 */
	private String definitionId;

	/**
	 * 任务id
	 */
	private String taskId;

	/**
	 * 系统列表
	 */
	//private List<FlowSys> sysList;

	private static String taskClass = "com.apache.flow.taskHandler.AssignTask";

	private String deployId;//流程发布Id

	private String status;

	/**
	 * 流程图片
	 */
	private File imgUpload;
	/**
	 * 绑定表单
	 */
	private String formUrl;

	private String imgUploadContentType;

	private String rows;

	private String page;

	private String sysEname;//系统标识
	private String sysCname;//系统中文名称
	private String sysId;//系统标识
	private String nodeName;
	private String nodeId;

	private String id;

	private List<FlowTaskUrl> urlList;

	private FlowDeploy flowDeploy;

	private List<Org> orgList;

	private String orgId;
	private String orgCname;
	private String operType;//操作者类型：1.operator-运营商 2.其它值---机构用户

	/**
	 * 发布流程
	 * @return String
	 * @throws IOException 
	 */
	public String upload() throws IOException {
		logger.info("流程发布: imgUploadFileName=" + imgUploadFileName + "    uploadFileName=" + uploadFileName
				+ "   sysEname=" + sysEname);

		JSONObject jvo = new JSONObject();
		if (StrUtil.isNull(orgId)) {
			jvo.put("flag", "F");
			jvo.put("msg", "操作失败，orgId为空！");
			Struts2Utils.getResponse().setContentType("text/html;charset=UTF-8");
			Struts2Utils.getResponse().getWriter().write(jvo.toString());
			return null;
		}

		//管理流程定义
		String realPath = getFlowDir();
		String fileTime = null;
		try {
			if (upload != null) {
				try {
					FileInputStream i = new FileInputStream(upload);
					FileInputStream png = new FileInputStream(imgUpload);
					InputStream p = png;
					String nowTime = DateUtil.getNowTime();
					fileTime = nowTime.replaceAll(":", "_");
					saveFile(imgUpload, realPath, fileTime + imgUploadFileName);
					saveFile(upload, realPath, fileTime + uploadFileName);

					FlowDeploy flowDeploy = new FlowDeploy();
					flowDeploy.setFileName(fileTime + uploadFileName);
					flowDeploy.setPictureName(fileTime + imgUploadFileName);
					flowDeploy.setSysEname(sysEname);
					flowDeploy.setSysCname(sysCname);
					flowDeploy.setCreateTime(nowTime);
					flowDeploy.setOrgId(orgId);
					flowDeploy.setOrgCname(orgCname);

					flowDeploy.setProcessStatus("1");
					deployId = flowDeployManager.saveOrUpdate(flowDeploy);

				} catch (Exception e) {
					e.printStackTrace();
				}
			}

			jvo.put("flag", "T");
			jvo.put("msg", "发布流程成功！");
			jvo.put("uploadFileName", fileTime + uploadFileName);
			jvo.put("imgUploadFileName", fileTime + imgUploadFileName);
			jvo.put("deployId", deployId);

			Struts2Utils.getResponse().setContentType("text/html;charset=UTF-8");
			Struts2Utils.getResponse().getWriter().write(jvo.toString());
			logger.info("流程发布成功,deployId=" + deployId);
		} catch (FileNotFoundException e) {
			e.printStackTrace();
			showError("发布流程失败！");
		} catch (IOException e) {
			e.printStackTrace();
			showError("发布流程失败！");
		}
		return null;
	}

	/**
	 * 
	 * @return
	 * @throws Exception
	 * <p>@description 流程更改
	 * <p>@version 1.0
	 * <p>@author lvcai
	 * <p>@update 2013-11-18 下午02:32:06
	 */
	public String edit() throws Exception {
		//管理流程定义
		String realPath = getFlowDir();
		String fileTime = null;
		try {
			if (upload != null) {

				InputStream i = new FileInputStream(upload);
				InputStream png = new FileInputStream(imgUpload);

				String nowTime = DateUtil.getNowTime();
				fileTime = nowTime.replaceAll(":", "__");//用两个下划线来区分是修改的文件

				saveFile(imgUpload, realPath, fileTime + imgUploadFileName);
				saveFile(upload, realPath, fileTime + uploadFileName);

				RepositoryService repositoryService = processEngine.getRepositoryService();
				//获得该流程相关资源名称
				Set<String> resouceNames = repositoryService.getResourceNames(deploymentId);
				//流程定义文件名称
				String processName = null;

				//流程图文件名称
				String pngName = null;
				//TODO
				//目前认为取流程图片名称方法
				for (String name : resouceNames) {
					if (name.toLowerCase().endsWith(".xml")) {
						processName = name;
					} else if (name.toLowerCase().endsWith(".png")) {
						pngName = name;
					}
				}

				RepositoryServiceImpl impl = (RepositoryServiceImpl) repositoryService;
				//impl.updateDeploymentResource(deploymentId,pngName, new ByteArrayInputStream(new byte[png.available()]));
				//InputStream oldXml =  repositoryService.getResourceAsStream(deploymentId,processName);

				DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
				factory.setNamespaceAware(true);
				factory.setCoalescing(true);
				factory.setIgnoringComments(true);
				DocumentBuilder documentBuilder = null;
				documentBuilder = factory.newDocumentBuilder();
				Document doc = documentBuilder.parse(i);
				doc.setXmlStandalone(true);//去除生成的xml  standalone="no"
				Transformer transformer = TransformerFactory.newInstance().newTransformer();
				transformer.setOutputProperty(OutputKeys.INDENT, "yes");
				StreamResult result = new StreamResult(new StringWriter());
				DOMSource source = new DOMSource(doc);
				transformer.transform(source, result);
				String updatedProcessXml = result.getWriter().toString();
				//				DeploymentImpl deployImpl = (DeploymentImpl) repositoryService 
				//				.createDeployment(); 
				//DeploymentImpl deployImpl=EnvironmentImpl.getFromCurrent(DeploymentImpl.class);

				//deployImpl.addResourceFromInputStream(processName, new ByteArrayInputStream(updatedProcessXml.getBytes()));

				//processEngine.execute(new MyUpdateDeploymentResourceCmd(deploymentId, processName, new ByteArrayInputStream(updatedProcessXml.getBytes())));

				//修改流程定义文件与流程图
				OracleConnection.blobReplace(upload, processName, imgUpload, pngName);

				//processEngine.execute(new MyUpdateDeploymentResourceCmd(deploymentId, pngName,  new ByteArrayInputStream(bys)));

				//impl.updateDeploymentResource(deploymentId, fileTime+imgUploadFileName, png);

				//				FlowDeploy flowDeploy=new FlowDeploy();
				//				flowDeploy.setFileName(fileTime+uploadFileName);
				//				flowDeploy.setPictureName(fileTime+imgUploadFileName);
				//				flowDeploy.setSysEname(sysEname);
				//				flowDeploy.setCreateTime(nowTime);
				//					
				//				flowDeploy.setProcessStatus("1");
				//				deployId=flowDeployManager.saveOrUpdate(flowDeploy);

			}
			JSONObject jvo = new JSONObject();
			jvo.put("flag", "T");
			jvo.put("msg", "发布流程成功！");
			jvo.put("uploadFileName", fileTime + uploadFileName);
			jvo.put("imgUploadFileName", fileTime + imgUploadFileName);
			jvo.put("deployId", deployId);

			Struts2Utils.getResponse().setContentType("text/html;charset=UTF-8");
			Struts2Utils.getResponse().getWriter().write(jvo.toString());
			logger.info("流程发布成功,deployId=" + deployId);
		} catch (FileNotFoundException e) {
			e.printStackTrace();
			showError("发布流程失败！");
		} catch (IOException e) {
			e.printStackTrace();
			showError("发布流程失败！");
		} catch (Exception e) {
			e.printStackTrace();
			showError("流程修改时间！");
		}
		return null;
	}

	/**
	 * 
	 * @return
	 * @throws UctLockException
	 * <p>@description 跳转到流程设计页面
	 * <p>@version 1.0
	 * <p>@author lvcai
	 * <p>@update 2013-7-29 下午05:36:21
	 */
	public String design() throws Exception {
		/*logger.info("跳转到流程设计页面  sysEname=" + sysEname);
		FlowSys sys = flowSysManager.getFlowSysBySysEname(sysEname);
		urlList = flowTaskUrlManager.getFlowTaskUrlBySysId(sys.getSysId());*/
		return "design";
	}

	/**
	 * 
	 * @return
	 * @throws Exception
	 * <p>@description 预览流程图
	 * <p>@version 1.0
	 * <p>@author lvcai
	 * <p>@update 2013-9-25 上午11:38:33
	 */
	public String viewPic() throws Exception {
		logger.info("预览流程图  deploymentId=" + deploymentId);
		return "pic";
	}

	/**
	 * 
	 * @return
	 * <p>@description 流程发布方法
	 * <p>@version 1.0
	 * <p>@author lvcai
	 * <p>@update 2013-7-29 下午05:35:55
	 */
	public String deploy() {
		logger.info("流程发布方法   imgUploadFileName=" + imgUploadFileName + "    uploadFileName=" + uploadFileName);
		String info = null;
		//管理流程定义
		RepositoryService repositoryService = processEngine.getRepositoryService();

		String realPath = getFlowDir();
		File imgUpload = new File(realPath + File.separator + imgUploadFileName);

		String updatedProcessXml = "";
		try {
			InputStream inputStream = new FileInputStream(realPath + File.separator + uploadFileName);
			DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
			factory.setNamespaceAware(true);
			factory.setCoalescing(true);
			factory.setIgnoringComments(true);
			DocumentBuilder documentBuilder = null;
			documentBuilder = factory.newDocumentBuilder();
			Document doc = null;
			doc = documentBuilder.parse(inputStream);
			Transformer transformer = TransformerFactory.newInstance().newTransformer();
			transformer.setOutputProperty(OutputKeys.INDENT, "yes");
			StreamResult result = new StreamResult(new StringWriter());
			DOMSource source = new DOMSource(doc);
			transformer.transform(source, result);
			updatedProcessXml = result.getWriter().toString();
		} catch (Exception e) {
			e.printStackTrace();
		}
		logger.info("deployFile : " + updatedProcessXml);
		String DeploymentId = null;
		try {
			DeploymentId = repositoryService.createDeployment()
					.addResourceFromString(uploadFileName, updatedProcessXml.trim()).addResourceFromFile(imgUpload)
					.deploy();
		} catch (org.jbpm.api.JbpmException e) {
			if (e.getMessage().contains("already exists")) {
				info = "流程version 或key 已经存在，请修改后重新发布";
			} else if (e.getMessage().contains("must have one of")) {
				info = "流程发布失败,流程定义文件中判断节点处未设置表达式";
			} else {
				info = "流程发布失败";
			}
			e.printStackTrace();
		}

		if (DeploymentId != null) {
			ProcessDefinition pd = repositoryService.createProcessDefinitionQuery().deploymentId(DeploymentId)
					.uniqueResult();
			FlowDeploy flowDeploy = flowDeployManager.getById(deployId);
			flowDeploy.setProcessDeploymentId(DeploymentId);
			flowDeploy.setProcessDefinitionId(pd.getId());
			flowDeploy.setProcessName(pd.getName());
			flowDeploy.setProcessSpare(pd.getDescription());
			flowDeploy.setProcessVersion(String.valueOf(pd.getVersion()));
			flowDeploy.setProcessKey(pd.getKey());
			flowDeploy.setProcessStatus("3");
			flowDeployManager.saveOrUpdate(flowDeploy);
			info = "流程发布成功";
			logger.info("流程发布信息保存成功，processDeploymentId=" + DeploymentId);
		}
		Struts2Utils.getRequest().setAttribute("info", info);
		return "init";
	}

	/**
	 * 得到流程实例流程图
	 * @return String
	 * @throws IOException 
	 */
	public String getProcessPic() throws IOException {
		//管理流程定义
		RepositoryService repositoryService = processEngine.getRepositoryService();
		//获得该流程相关资源名称
		Set<String> resouceNames = repositoryService.getResourceNames(deploymentId);
		//流程图名称
		String picName = null;
		//TODO
		//目前认为取流程图片名称方法
		for (String name : resouceNames) {
			if (name.toLowerCase().endsWith(".png") || name.toLowerCase().endsWith(".tmp")) {
				picName = name;
				break;
			}
		}
		logger.info("流程图名称：" + picName);

		InputStream inputStream = repositoryService.getResourceAsStream(deploymentId, picName);

		byte[] b = new byte[1024];
		int len = -1;
		while ((len = inputStream.read(b, 0, 1024)) != -1) {
			Struts2Utils.getResponse().getOutputStream().write(b, 0, len);
		}
		return null;
	}

	/**
	 * 
	 * @return
	 * <p>@description 修改节点信息
	 * <p>@version 1.0
	 * <p>@author lvcai
	 * <p>@update 2013-7-29 下午05:35:22
	 */
	public String updateNodes() {
		RepositoryService repositoryService = processEngine.getRepositoryService();

		//获得该流程相关资源名称
		Set<String> resouceNames = repositoryService.getResourceNames(deploymentId);
		//流程图名称
		String processName = null;

		//目前认为取流程图片名称方法
		for (String name : resouceNames) {
			if (name.toLowerCase().endsWith(".xml")) {
				processName = name;
				break;
			}
		}
		logger.info("流程图名称：" + processName);
		//获得流程图流
		InputStream inputStream = repositoryService.getResourceAsStream(deploymentId, processName);

		List<Map<String, String>> aaxml = new ArrayList<Map<String, String>>();
		try {
			DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
			factory.setNamespaceAware(true);
			factory.setCoalescing(true);
			factory.setIgnoringComments(true);
			DocumentBuilder documentBuilder = null;
			documentBuilder = factory.newDocumentBuilder();
			Document doc = null;
			doc = documentBuilder.parse(inputStream);
			Element root = doc.getDocumentElement();
			NodeList nodes = root.getElementsByTagName("task");

			for (int i = 0; i < nodes.getLength(); i++) {
				String users = null;
				String group = null;
				String assignee = null;
				String swimlane = null;
				String type = null;
				Element ele = (Element) nodes.item(i);
				String name = ele.getAttribute("name");
				String loc = ele.getAttribute("g");
				String action = ele.getAttribute("form");
				String ss0 = ele.getAttribute("candidate-users");
				String ss1 = ele.getAttribute("candidate-groups");
				String ss2 = ele.getAttribute("assignee");
				String ss3 = ele.getAttribute("swimlane");
				//NodeFormVo nodeFormVo = new NodeFormVo();

				if (ss0 != null & !"".equals(ss0)) {
					users = ss0;
					type = "users";
				} else if (ss1 != null & !"".equals(ss1)) {
					group = ss1;
					type = "role";
				} else if (ss2 != null & !"".equals(ss2)) {
					assignee = ss2;
				} else if (ss3 != null & !"".equals(ss3)) {
					swimlane = ss3;
				}

				Map<String, String> m = new HashMap<String, String>();

				String[] locs = loc.split(",");
				m.put("height", locs[3]);
				m.put("width", locs[2]);
				m.put("left", locs[0]);
				m.put("top", locs[1]);
				m.put("id", "" + i + 1);
				m.put("name", name);
				m.put("action", action);
				m.put("users", users);
				m.put("group", group);
				m.put("assignee", assignee);
				m.put("swimlane", swimlane);
				m.put("type", type);
				aaxml.add(m);

			}

		} catch (Exception e) {
			e.printStackTrace();
		}
		JSONObject json = new JSONObject();
		json.put("total", aaxml.size());
		json.put("rows", aaxml);
		try {
			Struts2Utils.getResponse().getWriter().write(json.toString());
		} catch (IOException e) {
			logger.debug("得到流程实例数据错误" + e);
			e.printStackTrace();
		}

		return null;

	}

	/**
	 * 
	 * @return
	 * @throws UctSysException
	 * <p>@description 跳转到修改流程的方法
	 * <p>@version 1.0
	 * <p>@author lvcai
	 * <p>@update 2013-7-29 下午05:34:00
	 */
	public String toupdate() throws Exception {
		/*logger.info("跳转到修改流程的方法，sysEname=" + sysEname);
		FlowSys sys = flowSysManager.getFlowSysBySysEname(sysEname);
		urlList = flowTaskUrlManager.getFlowTaskUrlBySysId(sys.getSysId());*/
		return "toupdate";
	}

	/**
	 * 
	 * @return
	 * <p>@description 流程更新
	 * <p>@version 1.0
	 * <p>@author lvcai
	 * <p>@update 2013-7-29 下午05:34:29
	 */
	public String update() {
		RepositoryService repositoryService = processEngine.getRepositoryService();
		//获得该流程相关资源名称
		Set<String> resouceNames = repositoryService.getResourceNames(deploymentId);
		//流程图名称
		String processName = null;
		//TODO
		//目前认为取流程图片名称方法
		for (String name : resouceNames) {
			if (name.toLowerCase().endsWith(".xml")) {
				processName = name;
				break;
			}
		}
		logger.debug("流程图名称：" + processName);
		//获得流程图流
		InputStream inputStream = repositoryService.getResourceAsStream(deploymentId, processName);

		String updatedProcessXml = "";
		try {
			DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
			factory.setNamespaceAware(true);
			factory.setCoalescing(true);
			factory.setIgnoringComments(true);
			DocumentBuilder documentBuilder = null;
			documentBuilder = factory.newDocumentBuilder();
			Document doc = null;
			doc = documentBuilder.parse(inputStream);

			Transformer transformer = TransformerFactory.newInstance().newTransformer();
			transformer.setOutputProperty(OutputKeys.INDENT, "yes");
			StreamResult result = new StreamResult(new StringWriter());
			DOMSource source = new DOMSource(doc);
			transformer.transform(source, result);
			updatedProcessXml = result.getWriter().toString();
			flowInitManager.update(deploymentId, processName, updatedProcessXml);

			ProcessDefinition pd = repositoryService.createProcessDefinitionQuery().deploymentId(deploymentId)
					.uniqueResult();
			FlowDeploy flowDeploy = flowDeployManager.getById(deployId);
			flowDeploy.setProcessDeploymentId(deploymentId);
			flowDeploy.setProcessDefinitionId(pd.getId());
			flowDeploy.setProcessName(pd.getName());
			flowDeploy.setProcessSpare(pd.getDescription());
			flowDeploy.setProcessVersion(String.valueOf(pd.getVersion()));
			flowDeploy.setProcessKey(pd.getKey());
			flowDeployManager.saveOrUpdate(flowDeploy);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			logger.info("deployFile : " + updatedProcessXml);

		}

		return "init";

	}

	/**
	 * 代办
	 * @return
	 * @throws IOException 
	 */
	public String agent() throws IOException {
		TaskService taskService = processEngine.getTaskService();
		try {
			if (taskId != null) {
				Enumeration<String> names = Struts2Utils.getRequest().getParameterNames();
				while (names.hasMoreElements()) {
					String key = names.nextElement();
					String v = Struts2Utils.getRequest().getParameter(key);
					if (key != null && key.equals("agentUserId")) {
						taskService.assignTask(taskId, v);
						break;
					}
				}
			}
			showOk("分配成功!");
		} catch (Exception e) {
			showError("分配失败!");
			e.printStackTrace();
		}
		return null;
	}

	/**
	 * 启动流程实例
	 * <p>启动流程实例应该在业务系统中执行
	 * @return String
	 * @throws IOException 
	 */
	public String start() throws IOException {
		//管理流程实例
		try {
			ExecutionService executionService = processEngine.getExecutionService();
			Map<String, String> variables = new HashMap<String, String>();
			Enumeration<String> names = Struts2Utils.getRequest().getParameterNames();
			while (names.hasMoreElements()) {
				String key = names.nextElement();
				variables.put(key, Struts2Utils.getRequest().getParameter(key));
			}
			executionService.startProcessInstanceById(definitionId, variables);
			showOk("启动流程成功！");
		} catch (Exception e) {
			logger.debug("启动" + definitionId + "的流程实例错误");
			showError("启动流程失败！");
			e.printStackTrace();
		}

		return null;
	}

	/**
	 * 
	 * @return
	 * <p>@description 列表显示已经发布的流程
	 * <p>@version 1.0
	 * <p>@author lvcai
	 * <p>@update 2013-7-29 下午05:42:03
	 */
	public String data() {
		int pageNo = NumberUtils.getInt(page, 1);
		int pageSize = NumberUtils.getInt(rows, 15);
		int startIndex = (pageNo - 1) * pageSize;

		Struts2Utils.getResponse().setContentType("text/html;charset=UTF-8");
		RepositoryService repositoryService = processEngine.getRepositoryService();

		ProcessDefinitionQuery processDefinitionQuery = repositoryService.createProcessDefinitionQuery();
		int sumCount = processDefinitionQuery.list().size();

		List<ProcessDefinition> pdList = processDefinitionQuery.orderDesc(ProcessDefinitionQuery.PROPERTY_VERSION)
				.page(startIndex, pageSize).list();
		List<ProcessDefinitionVo> pdVolist = new ArrayList<ProcessDefinitionVo>();
		for (ProcessDefinition pd : pdList) {
			ProcessDefinitionVo pdVo = new ProcessDefinitionVo();
			pdVo.setId(pd.getId());
			pdVo.setName(pd.getName());
			pdVo.setKey(pd.getKey());
			pdVo.setVersion(pd.getVersion());
			pdVo.setDeploymentId(pd.getDeploymentId());
			pdVo.setDescription(pd.getDescription());
			pdVo.setState(pd.isSuspended() ? "suspended" : "active");//判断流程是否挂起
			pdVolist.add(pdVo);
		}
		JSONObject json = new JSONObject();
		json.put("total", sumCount);
		json.put("rows", pdVolist);
		try {
			Struts2Utils.getResponse().getWriter().write(json.toString());
		} catch (IOException e) {
			logger.debug("得到流程定义数据错误" + e);
			e.printStackTrace();
		}
		return null;
	}

	/**
	 * 
	 * @return
	 * <p>@description 列表显示发布的流程
	 * <p>@version 1.0
	 * <p>@author lvcai
	 * <p>@update 2013-7-29 下午05:41:17
	 */
	public String flowData() {
		int pageNo = NumberUtils.getInt(page, 1);
		int pageSize = NumberUtils.getInt(rows, 15);

		if (StrUtil.isNull(orgId) && !"operator".equals(operType))
			return null;

		Struts2Utils.getResponse().setContentType("text/html;charset=UTF-8");
		FlowDeployvo vo = new FlowDeployvo();
		vo.setOrgId(orgId);
		Page<FlowDeployvo> page = flowDeployManager.getFlowDeployInfo(vo, pageNo, pageSize);
		JSONObject json = new JSONObject();
		json.put("total", page.getTotalCount());
		json.put("rows", page.getResult());
		try {
			Struts2Utils.getResponse().getWriter().write(json.toString());
		} catch (IOException e) {
			logger.debug("得到流程定义数据错误" + e);
			e.printStackTrace();
		}

		return null;
	}

	/**
	 * 
	 * @return
	 * @throws IOException
	 * <p>@description 删除流程
	 * <p>@version 1.0
	 * <p>@author lvcai
	 * <p>@update 2013-7-29 下午05:42:38
	 */
	public String delete() throws IOException {
		try {
			logger.info("流程定义删除：deployId=" + deployId + "    deploymentId=" + deploymentId);
			if (status != null && !"1".equals(status.trim())) {
				RepositoryService repositoryService = processEngine.getRepositoryService();
				//此删除会级联删除，即包括发布记录、流程定义记录、流程处理记录、历史数据记录
				repositoryService.deleteDeploymentCascade(deploymentId);
			}
			FlowDeploy deploy = flowDeployManager.getById(deployId);
			flowDeployManager.delete(deploy);

			showOk("删除成功！");
		} catch (IOException e) {
			e.printStackTrace();
			showError("删除失败！");
		}
		return null;
	}

	/**
	 * 
	 * @return
	 * @throws IOException
	 * <p>@description 恢复流程
	 * <p>@version 1.0
	 * <p>@author lvcai
	 * <p>@update 2013-7-29 下午05:42:54
	 */
	public String resume() throws IOException {
		try {
			logger.info("流程恢复：deployId=" + deployId + "    deploymentId=" + deploymentId);
			RepositoryService repositoryService = processEngine.getRepositoryService();
			repositoryService.resumeDeployment(deploymentId);
			FlowDeploy deploy = flowDeployManager.getById(deployId);
			deploy.setProcessStatus("3");
			flowDeployManager.saveOrUpdate(deploy);
			showOk("恢复流程定义成功！");
		} catch (IOException e) {
			e.printStackTrace();
			showError("恢复流程定义失败！");
		} catch (JbpmException e) {
			showError("该流程定义状态为正常,不用再次恢复！");
		}
		return null;
	}

	/**
	 * 
	 * @return
	 * @throws IOException
	 * <p>@description 挂起流程
	 * <p>@version 1.0
	 * <p>@author lvcai
	 * <p>@update 2013-7-29 下午05:42:54
	 */
	public String suspend() throws IOException {
		try {
			logger.info("流程恢复：deployId=" + deployId + "    deploymentId=" + deploymentId);
			RepositoryService repositoryService = processEngine.getRepositoryService();
			repositoryService.suspendDeployment(deploymentId);

			FlowDeploy deploy = flowDeployManager.getById(deployId);
			deploy.setProcessStatus("2");
			flowDeployManager.saveOrUpdate(deploy);
			showOk("挂起流程定义成功！");
		} catch (IOException e) {
			e.printStackTrace();
			showError("挂起流程定义失败！");
		} catch (JbpmException e) {
			showError("该流程定义状态已经为已挂起,不能再次挂起");
		}
		return null;
	}

	/**
	 * 默认方法
	 */
	public String execute() {
		return SUCCESS;
	}

	public void orgTreeJson() {
		this.logger.info("--------开始执行查询机构树形json方法-----orgId=" + orgId);

		Map<String, Object> paramMap = new HashMap<String, Object>();
		if (StrUtil.isNull(id)) {
			paramMap.put("fatherId", "0");
		} else {
			paramMap.put("fatherId", id);
		}
		String info = FlowClientUtil.getOrgListJson(paramMap);
		if (info != null && !"".equals(info)) {
			JSONArray ja = JSONArray.fromObject(info);
			try {
				logger.info("获取机构：" + ja.toString());
				Struts2Utils.getResponse().getWriter().write(ja.toString());
			} catch (IOException e) {
				logger.debug("远程获取机构失败错误" + e);
				e.printStackTrace();
			}
		} else {
			logger.info("fatherId=" + id + "下没有获取到机构");
		}
	}

	/**
	 * description:远程查询当前机构下的角色
	 * @return  
	 * @author mhguo 
	 * @update 2016-2-1
	 */
	public String getRoleListJson() {
		this.logger.info("----[{查询角色列表方法开始}]---:orgId=" + orgId);
		Struts2Utils.getResponse().setContentType("text/html;charset=UTF-8");
		JSONArray jary = null;

		if (StrUtil.isNull(orgId)) {
			this.logger.error("orgId不能为空！");
			jary = new JSONArray();
			try {
				Struts2Utils.getResponse().getWriter().write(jary.toString());
			} catch (IOException e) {
				e.printStackTrace();
			}
			return null;
		}
		Map<String, Object> paramMap = new HashMap<String, Object>();
		paramMap.put("orgId", orgId);
		List<Role> roleList = FlowClientUtil.getRoleList(paramMap);

		if (roleList != null) {
			jary = JSONArray.fromObject(roleList);
		} else {
			jary = new JSONArray();
		}

		try {
			logger.info("远程查询角色：" + jary.toString());
			Struts2Utils.getResponse().getWriter().write(jary.toString());
		} catch (IOException e) {
			logger.debug("远程查询角色发生错误：" + e);
			e.printStackTrace();
		}
		return null;
	}

	public String getActionListJson() {
		String roleId = Struts2Utils.getRequest().getParameter("roleId");
		this.logger.info("----[{资源列表方法开始}]---:roleId=" + roleId);
		Struts2Utils.getResponse().setContentType("text/html;charset=UTF-8");
		JSONArray jary = null;

		/*	if (StrUtil.isNull(roleId)) {
				this.logger.error("roleId不能为空！");
				jary = new JSONArray();
				try {
					Struts2Utils.getResponse().getWriter().write(jary.toString());
				} catch (IOException e) {
					e.printStackTrace();
				}
				return null;
			}*/
		Map<String, Object> paramMap = new HashMap<String, Object>();
		paramMap.put("actType", "action");//过滤掉菜单项
		if (StrUtil.isNotNull(roleId)) {
			paramMap.put("roleId", roleId);
		} else if (StrUtil.isNotNull(sysEname)) {
			paramMap.put("sysEname", sysEname);
		} else {
			this.logger.error("roleId和sysEname不能都为空");
			jary = new JSONArray();
			try {
				Struts2Utils.getResponse().getWriter().write(jary.toString());
			} catch (IOException e) {
				e.printStackTrace();
			}
			return null;
		}

		List<Act> actList = FlowClientUtil.getActList(paramMap);

		if (actList != null) {
			jary = JSONArray.fromObject(actList);
		} else {
			jary = new JSONArray();
		}

		try {
			logger.info("远程查询权限资源：" + jary.toString());
			Struts2Utils.getResponse().getWriter().write(jary.toString());
		} catch (IOException e) {
			logger.debug("远程查询权限资源发生错误：" + e);
			e.printStackTrace();
		}
		return null;
	}

	/**
	 * 
	 * 
	 * <p>@description 流程发布后，从数据库中获取流程定义信息
	 * <p>@version 1.0
	 * <p>@author lvcai
	 * <p>@update 2013-7-29 下午05:44:13
	 */
	public void deployNodes() {
		List<Map<String, String>> aaxml = new ArrayList<Map<String, String>>();
		String realPath = getFlowDir();
		try {
			InputStream inputStream = new FileInputStream(realPath + File.separator + uploadFileName);
			DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
			factory.setNamespaceAware(true);
			factory.setCoalescing(true);
			factory.setIgnoringComments(true);
			DocumentBuilder documentBuilder = null;
			documentBuilder = factory.newDocumentBuilder();
			Document doc = null;
			doc = documentBuilder.parse(inputStream);
			Element root = doc.getDocumentElement();
			NodeList nodes = root.getElementsByTagName("task");

			for (int i = 0; i < nodes.getLength(); i++) {
				Element ele = (Element) nodes.item(i);
				ele.setAttribute("candidate-groups", "");
				String name = ele.getAttribute("name");
				String form = ele.getAttribute("form");
				String loc = ele.getAttribute("g");
				Map<String, String> m = new HashMap<String, String>();

				String[] locs = loc.split(",");

				m.put("height", locs[3]);
				m.put("width", locs[2]);
				m.put("left", locs[0]);
				m.put("top", locs[1]);
				m.put("id", "" + i + 1);
				m.put("name", name);
				aaxml.add(m);
			}

		} catch (Exception e) {
			e.printStackTrace();
		}
		JSONObject json = new JSONObject();
		json.put("total", aaxml.size());
		json.put("rows", aaxml);
		try {
			Struts2Utils.getResponse().getWriter().write(json.toString());
		} catch (IOException e) {
			logger.debug("得到流程实例数据错误" + e);
			e.printStackTrace();
		}
	}

	//获取登陆用户信息
	@SuppressWarnings("rawtypes")
	private LoginUser getLoginUserInfo() {
		//单点登录用户信息
		String tokenId = PassportHelper.getInstance().getCurrCookie(Struts2Utils.getRequest());

		//session中已经存在用户登录对象，则从session中获取
		Object loginObj = Struts2Utils.getRequest().getSession().getAttribute("loginUser");
		LoginUser loginUser = null;
		if (loginObj == null) {
			loginUser = PBOSSOTools.getLoginUserFromUserCenterSso(tokenId);
		} else {
			loginUser = PBOSSOTools.getLoginUser(Struts2Utils.getRequest());
		}
		if (loginUser == null) {
			this.logger.info("TestUCAction中未获取到loginUser登录信息:" + loginUser);
		}
		return loginUser;
	}

	/**
	 * 
	 * 
	 * <p>@description 远程获取用户信息
	 * <p>@version 1.0
	 * <p>@author lvcai
	 * <p>@update 2013-7-29 下午05:45:20
	 */
	public void getIdentityJsonUser() {
		logger.info("远程获取所有用户信息");
		LoginUser loginUser = this.getLoginUserInfo();
		JSONObject json = new JSONObject();
		json.put("total", 0);
		json.put("rows", new ArrayList());

		String keyword = Struts2Utils.getParameter("keyword");
		String orgId = Struts2Utils.getRequest().getParameter("orgId");
		if (StrUtil.isNull(orgId) && (null != loginUser))
			orgId = loginUser.getOrgId();

		Map<String, Object> paramMap = new HashMap<String, Object>();

		if (StrUtil.isNotNull(keyword)) {
			paramMap.put("userEname", keyword.trim());
		}
		if (StrUtil.isNotNull(orgId)) {
			paramMap.put("orgId", orgId);
		} else {
			paramMap.put("userAll", "userAll");
		}

		List list = null;
		list = FlowClientUtil.getUserList(paramMap);

		if (null != list) {
			json.put("total", list.size());
			json.put("rows", list);
		}

		try {
			logger.info("获取用户：" + json.toString());
			Struts2Utils.getResponse().getWriter().write(json.toString());
		} catch (IOException e) {
			logger.debug("得到用户数据错误" + e);
			e.printStackTrace();
		}

	}

	/**
	 * 
	 * 
	 * <p>@description 远程获取系统角色
	 * <p>@version 1.0
	 * <p>@author lvcai
	 * <p>@update 2013-7-29 下午05:45:47
	 */
	public void getIdentityJsonRole() {
		logger.info("远程获取系统角色信息  sysEname=" + sysEname);
		sysEname = sysEname == null ? "" : sysEname;
		Map<String, String> paramMap = new HashMap<String, String>();
		paramMap.put("role.sysEname", sysEname);
		List list = FlowClientUtil.getAllRoleList(paramMap);
		JSONObject json = new JSONObject();
		json.put("total", list.size());
		json.put("rows", list);
		try {
			logger.info("获取角色：" + json.toString());
			Struts2Utils.getResponse().getWriter().write(json.toString());
		} catch (IOException e) {
			logger.debug("得到流程实例数据错误" + e);
			e.printStackTrace();
		}
	}

	/**
	 * 
	 * @throws UnsupportedEncodingException
	 * <p>@description 远程获取系统权限
	 * <p>@version 1.0
	 * <p>@author lvcai
	 * <p>@update 2013-7-29 下午05:46:08
	 */
	public void getIdentityAct() throws UnsupportedEncodingException {
		sysEname = sysEname == null ? "" : sysEname;
		logger.info("远程获取系统权限信息  sysEname=" + sysEname + "  fatherId=" + id);
		id = id == null ? "" : id;
		String info = FlowClientUtil.getAllActList(id, sysEname);
		if (info != null && !"".equals(info)) {
			JSONArray ja = JSONArray.fromObject(info);
			try {
				logger.info("获取权限：" + ja.toString());
				Struts2Utils.getResponse().getWriter().write(ja.toString());
			} catch (IOException e) {
				logger.debug("远程获取权限失败错误" + e);
				e.printStackTrace();
			}
		} else {
			logger.info("系统" + sysEname + "下没有获取到权限");
		}

	}

	/**
	 * 
	 * @return
	 * @throws Exception
	 * <p>@description 跳转到发布界面
	 * <p>@version 1.0
	 * <p>@author lvcai
	 * <p>@update 2013-7-29 下午05:46:30
	 */
	public String toupload() throws Exception {
		//sysList = flowSysManager.getAllFlowSys();
		Map<String, Object> paramMap = new HashMap<String, Object>();
		paramMap.put("sysAll", "sysAll");
		List<Sys> sysList = FlowClientUtil.getSysList(paramMap);
		ActionContext.getContext().put("sysList", sysList);
		if ("operator".equals(operType)) {
			orgList = FlowClientUtil.getOrgList(null);
		}
		return "toupload";
	}

	/**
	 * 
	 * @return
	 * @throws Exception
	 * <p>@description 跳转到流程更改页面
	 * <p>@version 1.0
	 * <p>@author lvcai
	 * <p>@update 2013-11-18 下午02:08:14
	 */
	public String toEdit() throws Exception {

		return "edit";
	}

	/**
	 * 
	 * @return
	 * @throws Exception
	 * <p>@description 修改流程定义文件
	 * <p>@version 1.0
	 * <p>@author lvcai
	 * <p>@update 2013-7-29 下午05:47:46
	 */
	public String updateFile() throws Exception {
		//FlowSys sys = flowSysManager.getFlowSysBySysEname(sysEname);

		logger.info("修改流程定义文件:  sysEname=" + sysEname);
		if (StrUtil.isNull(sysEname)) {
			this.showError("sysEname不能为空！");
		}

		String sys_url = null;
		ResultEntity resultEntity = UctCoreClient.getSysByProperty("sysEname", sysEname);
		if (resultEntity == null) {
			this.logger.error("请求失败,通信异常!");
			this.showError("请求失败,通信异常!");
		} else {
			System.out.println("========响应=========Result:" + resultEntity.getResult() + " ,Message : "
					+ resultEntity.getMessage());
			if ("true".equals(resultEntity.getResult())) {
				Sys sysObj = (Sys) resultEntity.getEntity();
				sys_url = sysObj.getDelStatus();
			} else {
				this.logger.error("查询系统信息失败！" + resultEntity.getMessage());
				this.showError("查询系统信息失败！" + resultEntity.getMessage());
			}
		}

		if (null != jsonnodes && !"".equals(jsonnodes)) {
			JSONArray jsonarray = JSONArray.fromObject(jsonnodes);
			JSONObject jsonObject = jsonarray.getJSONObject(0);
			//nodeName = new String(nodeName.getBytes("ISO8859-1"), "GBK");
			nodeName = URLDecoder.decode(nodeName, "UTF-8");
			String updatedProcessXml = "";
			String realPath = getFlowDir();
			try {
				InputStream inputStream = new FileInputStream(realPath + File.separator + uploadFileName);
				DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
				factory.setNamespaceAware(true);
				factory.setCoalescing(true);
				factory.setIgnoringComments(true);
				DocumentBuilder documentBuilder = null;
				documentBuilder = factory.newDocumentBuilder();
				Document doc = null;
				doc = documentBuilder.parse(inputStream);
				Element root = doc.getDocumentElement();
				NodeList nodes = root.getElementsByTagName("task");
				for (int i = 0; i < nodes.getLength(); i++) {
					Element ele = (Element) nodes.item(i);
					String name = ele.getAttribute("name").trim();

					if (name.equalsIgnoreCase(nodeName.trim())) {
						NodeList handler_node = ele.getElementsByTagName("assignment-handler");
						Element el_handler = null;
						if (handler_node.getLength() == 0) {//设置
							el_handler = doc.createElement("assignment-handler");
							el_handler.setAttribute("class", FlowDeployAction.taskClass);
							ele.appendChild(el_handler);

							Element el_field_action = doc.createElement("field");
							el_field_action.setAttribute("name", "action");
							el_handler.appendChild(el_field_action);
							Element el_action_value = doc.createElement("string");
							el_action_value.setAttribute("value", sys_url + jsonObject.getString("action"));
							el_field_action.appendChild(el_action_value);

							//							Element el_field_rightType=doc.createElement("field");
							//							el_field_rightType.setAttribute("name", "rightType");
							//							el_handler.appendChild(el_field_rightType);
							//								Element el_rightType_value=doc.createElement("string");
							//								el_rightType_value.setAttribute("value", jsonObject.getString("type"));
							//								el_field_rightType.appendChild(el_rightType_value);
							//								
							//							Element el_field_rightValue=doc.createElement("field");
							//							el_field_rightValue.setAttribute("name", "rightValue");
							//							el_handler.appendChild(el_field_rightValue);
							//								Element el_rightValue_value=doc.createElement("string");
							//								el_rightValue_value.setAttribute("value", jsonObject.getString("identitySelID"));
							//								el_field_rightValue.appendChild(el_rightValue_value);

							Element el_field_actionActId = doc.createElement("field");
							el_field_actionActId.setAttribute("name", "actionActId");
							el_handler.appendChild(el_field_actionActId);
							Element el_actionActId_value = doc.createElement("string");
							el_actionActId_value.setAttribute("value", jsonObject.getString("actionActId"));
							el_field_actionActId.appendChild(el_actionActId_value);

							Element el_field_rightType = doc.createElement("field");
							el_field_rightType.setAttribute("name", "ownerValue");
							el_handler.appendChild(el_field_rightType);
							Element el_rightType_value = doc.createElement("string");
							el_rightType_value.setAttribute("value", jsonObject.getString("ownerValue"));
							el_field_rightType.appendChild(el_rightType_value);

							Element el_field_rightValue = doc.createElement("field");
							el_field_rightValue.setAttribute("name", "roleValue");
							el_handler.appendChild(el_field_rightValue);
							Element el_rightValue_value = doc.createElement("string");
							el_rightValue_value.setAttribute("value", jsonObject.getString("roleValue"));
							el_field_rightValue.appendChild(el_rightValue_value);

							Element el_field_sysEname = doc.createElement("field");
							el_field_sysEname.setAttribute("name", "sysEname");
							el_handler.appendChild(el_field_sysEname);
							Element el_sysEname_value = doc.createElement("string");
							el_sysEname_value.setAttribute("value", jsonObject.getString("sysEname"));
							el_field_sysEname.appendChild(el_sysEname_value);

							Element el_field_taskType = doc.createElement("field");
							el_field_taskType.setAttribute("name", "taskType");
							el_handler.appendChild(el_field_taskType);
							Element el_taskType_value = doc.createElement("string");
							el_taskType_value.setAttribute("value", jsonObject.getString("taskType"));
							el_field_taskType.appendChild(el_taskType_value);

							Element el_field_taskExeType = doc.createElement("field");
							el_field_taskExeType.setAttribute("name", "taskExeType");
							el_handler.appendChild(el_field_taskExeType);
							Element el_taskExeType_value = doc.createElement("string");
							el_taskExeType_value.setAttribute("value", jsonObject.getString("taskExeType"));
							el_field_taskExeType.appendChild(el_taskExeType_value);

							Element el_field_keepDays = doc.createElement("field");
							el_field_keepDays.setAttribute("name", "keepDays");
							el_handler.appendChild(el_field_keepDays);
							Element el_keepDays_value = doc.createElement("string");
							el_keepDays_value.setAttribute("value", jsonObject.getString("keepDays"));
							el_field_keepDays.appendChild(el_keepDays_value);

							Element el_field_crossDept = doc.createElement("field");
							el_field_crossDept.setAttribute("name", "crossDept");
							el_handler.appendChild(el_field_crossDept);
							Element el_crossDept_value = doc.createElement("string");
							el_crossDept_value.setAttribute("value", jsonObject.getString("crossDept"));
							el_field_crossDept.appendChild(el_crossDept_value);

						} else {//修改
							Node node_handler = handler_node.item(0);
							el_handler = (Element) node_handler;
							NodeList node_child = el_handler.getElementsByTagName("field");

							for (int k = 0; k < node_child.getLength(); k++) {
								Node node = node_child.item(k);

								Element el = (Element) node;

								if ("action".equals(el.getAttribute("name").trim())) {
									Element el_field_action = doc.createElement("field");
									el_field_action.setAttribute("name", "action");
									el_handler.appendChild(el_field_action);
									Element el_action_value = doc.createElement("string");
									el_action_value.setAttribute("value", sys_url + jsonObject.getString("action"));
									el_field_action.appendChild(el_action_value);

									el_handler.replaceChild(el_field_action, node);
								} else if ("actionActId".equals(el.getAttribute("name").trim())) {
									Element el_field_actionActId = doc.createElement("field");
									el_field_actionActId.setAttribute("name", "actionActId");
									el_handler.appendChild(el_field_actionActId);
									Element el_actionActId_value = doc.createElement("string");
									el_actionActId_value.setAttribute("value", jsonObject.getString("actionActId"));
									el_field_actionActId.appendChild(el_actionActId_value);

									el_handler.replaceChild(el_field_actionActId, node);
								} else if ("ownerValue".equals(el.getAttribute("name").trim())) {
									Element el_field_rightType = doc.createElement("field");
									el_field_rightType.setAttribute("name", "ownerValue");
									el_handler.appendChild(el_field_rightType);
									Element el_rightType_value = doc.createElement("string");
									el_rightType_value.setAttribute("value", jsonObject.getString("ownerValue"));
									el_field_rightType.appendChild(el_rightType_value);

									el_handler.replaceChild(el_field_rightType, node);
								} else if ("roleValue".equals(el.getAttribute("name").trim())) {
									Element el_field_rightValue = doc.createElement("field");
									el_field_rightValue.setAttribute("name", "roleValue");
									el_handler.appendChild(el_field_rightValue);
									Element el_rightValue_value = doc.createElement("string");
									el_rightValue_value.setAttribute("value", jsonObject.getString("roleValue"));
									el_field_rightValue.appendChild(el_rightValue_value);

									el_handler.replaceChild(el_field_rightValue, node);
								} else if ("sysEname".equals(el.getAttribute("name").trim())) {
									Element el_field_sysEname = doc.createElement("field");
									el_field_sysEname.setAttribute("name", "sysEname");
									el_handler.appendChild(el_field_sysEname);
									Element el_sysEname_value = doc.createElement("string");
									el_sysEname_value.setAttribute("value", jsonObject.getString("sysEname"));
									el_field_sysEname.appendChild(el_sysEname_value);

									el_handler.replaceChild(el_field_sysEname, node);
								} else if ("taskType".equals(el.getAttribute("name").trim())) {
									Element el_field_taskType = doc.createElement("field");
									el_field_taskType.setAttribute("name", "taskType");
									el_handler.appendChild(el_field_taskType);
									Element el_taskType_value = doc.createElement("string");
									el_taskType_value.setAttribute("value", jsonObject.getString("taskType"));
									el_field_taskType.appendChild(el_taskType_value);

									el_handler.replaceChild(el_field_taskType, node);
								} else if ("taskExeType".equals(el.getAttribute("name").trim())) {
									Element el_field_taskExeType = doc.createElement("field");
									el_field_taskExeType.setAttribute("name", "taskExeType");
									el_handler.appendChild(el_field_taskExeType);
									Element el_taskExeType_value = doc.createElement("string");
									el_taskExeType_value.setAttribute("value", jsonObject.getString("taskExeType"));
									el_field_taskExeType.appendChild(el_taskExeType_value);

									el_handler.replaceChild(el_field_taskExeType, node);
								} else if ("keepDays".equals(el.getAttribute("name").trim())) {
									Element el_field_keepDays = doc.createElement("field");
									el_field_keepDays.setAttribute("name", "keepDays");
									el_handler.appendChild(el_field_keepDays);
									Element el_keepDays_value = doc.createElement("string");
									el_keepDays_value.setAttribute("value", jsonObject.getString("keepDays"));
									el_field_keepDays.appendChild(el_keepDays_value);

									el_handler.replaceChild(el_field_keepDays, node);
								} else if ("crossDept".equals(el.getAttribute("name").trim())) {
									Element el_field_crossDept = doc.createElement("field");
									el_field_crossDept.setAttribute("name", "crossDept");
									el_handler.appendChild(el_field_crossDept);
									Element el_crossDept_value = doc.createElement("string");
									el_crossDept_value.setAttribute("value", jsonObject.getString("crossDept"));
									el_field_crossDept.appendChild(el_crossDept_value);

									el_handler.replaceChild(el_field_crossDept, node);
								}

							}

						}
						break;
					}

				}

				Transformer transformer = TransformerFactory.newInstance().newTransformer();
				transformer.setOutputProperty(OutputKeys.INDENT, "yes");
				StreamResult result = new StreamResult(new StringWriter());
				DOMSource source = new DOMSource(doc);
				transformer.transform(source, result);
				updatedProcessXml = result.getWriter().toString().trim();

				OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream(realPath + File.separator
						+ uploadFileName), "UTF-8");
				osw.write(updatedProcessXml.trim());
				osw.flush();
				osw.close();
				this.showOk("设置成功");
			} catch (Exception e) {
				this.showError("设置失败");
				e.printStackTrace();
			}

		} else {
			this.showError("提交信息有误");
			return null;
		}

		return null;
	}

	/**
	 * 
	 * @return
	 * @throws Exception
	 * <p>@description 流程发布后，修改流程定义信息
	 * <p>@version 1.0
	 * <p>@author lvcai
	 * <p>@update 2013-7-29 下午05:48:45
	 */
	public String updateXml() throws Exception {
		/*FlowSys sys = flowSysManager.getFlowSysBySysEname(sysEname);
		String sys_url = null;
		if (null != sys) {
			sys_url = sys.getSysUrl();
		} else {
			this.showError("该系统不存在");
		}*/
		logger.info("修改流程定义:  sysEname=" + sysEname);
		if (StrUtil.isNull(sysEname)) {
			this.showError("sysEname不能为空！");
		}
		String sys_url = null;
		ResultEntity resultEntity = UctCoreClient.getSysByProperty("sysEname", sysEname);
		if (resultEntity == null) {
			this.logger.error("请求失败,通信异常!");
			this.showError("请求失败,通信异常!");
		} else {
			System.out.println("========响应=========Result:" + resultEntity.getResult() + " ,Message : "
					+ resultEntity.getMessage());
			if ("true".equals(resultEntity.getResult())) {
				Sys sysObj = (Sys) resultEntity.getEntity();
				sys_url = sysObj.getDelStatus();
			} else {
				this.logger.error("查询系统信息失败！" + resultEntity.getMessage());
				this.showError("查询系统信息失败！" + resultEntity.getMessage());
			}
		}

		if (null != jsonnodes && !"".equals(jsonnodes)) {
			JSONArray jsonarray = JSONArray.fromObject(jsonnodes);
			JSONObject jsonObject = jsonarray.getJSONObject(0);
			//2015年4月16日13:15:05 yxcui
			nodeName = URLDecoder.decode(nodeName, "UTF-8");
			//nodeName = new String(nodeName.getBytes("ISO8859-1"), "GBK");
			RepositoryService repositoryService = processEngine.getRepositoryService();
			//获得该流程相关资源名称
			Set<String> resouceNames = repositoryService.getResourceNames(deploymentId);
			//流程图名称
			String processName = null;
			//TODO
			//目前认为取流程图片名称方法
			for (String name : resouceNames) {
				if (name.toLowerCase().endsWith(".xml")) {
					processName = name;
					break;
				}
			}
			logger.debug("流程图名称：" + processName);
			System.out.println(processName);
			//获得流程图流
			InputStream inputStream = repositoryService.getResourceAsStream(deploymentId, processName);

			String updatedProcessXml = "";
			try {
				DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
				factory.setNamespaceAware(true);
				factory.setCoalescing(true);
				factory.setIgnoringComments(true);
				DocumentBuilder documentBuilder = null;
				documentBuilder = factory.newDocumentBuilder();
				Document doc = null;
				doc = documentBuilder.parse(inputStream);
				Element root = doc.getDocumentElement();
				NodeList nodes = root.getElementsByTagName("task");
				for (int i = 0; i < nodes.getLength(); i++) {
					Element ele = (Element) nodes.item(i);
					String name = ele.getAttribute("name").trim();

					if (name.equalsIgnoreCase(nodeName.trim())) {
						NodeList handler_node = ele.getElementsByTagName("assignment-handler");
						Element el_handler = null;
						if (handler_node.getLength() == 0) {//设置
							el_handler = doc.createElement("assignment-handler");
							el_handler.setAttribute("class", FlowDeployAction.taskClass);
							ele.appendChild(el_handler);

							Element el_field_action = doc.createElement("field");
							el_field_action.setAttribute("name", "action");
							el_handler.appendChild(el_field_action);
							Element el_action_value = doc.createElement("string");
							el_action_value.setAttribute("value", sys_url + jsonObject.getString("action"));
							el_field_action.appendChild(el_action_value);

							Element el_field_actionActId = doc.createElement("field");
							el_field_actionActId.setAttribute("name", "actionActId");
							el_handler.appendChild(el_field_actionActId);
							Element el_actionActId_value = doc.createElement("string");
							el_actionActId_value.setAttribute("value", jsonObject.getString("actionActId"));
							el_field_actionActId.appendChild(el_actionActId_value);

							Element el_field_rightType = doc.createElement("field");
							el_field_rightType.setAttribute("name", "ownerValue");
							el_handler.appendChild(el_field_rightType);
							Element el_rightType_value = doc.createElement("string");
							el_rightType_value.setAttribute("value", jsonObject.getString("ownerValue"));
							el_field_rightType.appendChild(el_rightType_value);

							Element el_field_rightValue = doc.createElement("field");
							el_field_rightValue.setAttribute("name", "roleValue");
							el_handler.appendChild(el_field_rightValue);
							Element el_rightValue_value = doc.createElement("string");
							el_rightValue_value.setAttribute("value", jsonObject.getString("roleValue"));
							el_field_rightValue.appendChild(el_rightValue_value);

							Element el_field_sysEname = doc.createElement("field");
							el_field_sysEname.setAttribute("name", "sysEname");
							el_handler.appendChild(el_field_sysEname);
							Element el_sysEname_value = doc.createElement("string");
							el_sysEname_value.setAttribute("value", jsonObject.getString("sysEname"));
							el_field_sysEname.appendChild(el_sysEname_value);

							Element el_field_taskType = doc.createElement("field");
							el_field_taskType.setAttribute("name", "taskType");
							el_handler.appendChild(el_field_taskType);
							Element el_taskType_value = doc.createElement("string");
							el_taskType_value.setAttribute("value", jsonObject.getString("taskType"));
							el_field_taskType.appendChild(el_taskType_value);

							Element el_field_taskExeType = doc.createElement("field");
							el_field_taskExeType.setAttribute("name", "taskExeType");
							el_handler.appendChild(el_field_taskExeType);
							Element el_taskExeType_value = doc.createElement("string");
							el_taskExeType_value.setAttribute("value", jsonObject.getString("taskExeType"));
							el_field_taskExeType.appendChild(el_taskExeType_value);

							Element el_field_keepDays = doc.createElement("field");
							el_field_keepDays.setAttribute("name", "keepDays");
							el_handler.appendChild(el_field_keepDays);
							Element el_keepDays_value = doc.createElement("string");
							el_keepDays_value.setAttribute("value", jsonObject.getString("keepDays"));
							el_field_keepDays.appendChild(el_keepDays_value);

							Element el_field_crossDept = doc.createElement("field");
							el_field_crossDept.setAttribute("name", "crossDept");
							el_handler.appendChild(el_field_crossDept);
							Element el_crossDept_value = doc.createElement("string");
							el_crossDept_value.setAttribute("value", jsonObject.getString("crossDept"));
							el_field_crossDept.appendChild(el_crossDept_value);

						} else {//修改
							Node node_handler = handler_node.item(0);
							el_handler = (Element) node_handler;
							NodeList node_child = el_handler.getElementsByTagName("field");

							for (int k = 0; k < node_child.getLength(); k++) {
								Node node = node_child.item(k);

								Element el = (Element) node;

								if ("action".equals(el.getAttribute("name").trim())) {
									Element el_field_action = doc.createElement("field");
									el_field_action.setAttribute("name", "action");
									el_handler.appendChild(el_field_action);
									Element el_action_value = doc.createElement("string");
									el_action_value.setAttribute("value", sys_url + jsonObject.getString("action"));
									el_field_action.appendChild(el_action_value);

									el_handler.replaceChild(el_field_action, node);
								} else if ("actionActId".equals(el.getAttribute("name").trim())) {
									Element el_field_actionActId = doc.createElement("field");
									el_field_actionActId.setAttribute("name", "actionActId");
									el_handler.appendChild(el_field_actionActId);
									Element el_actionActId_value = doc.createElement("string");
									el_actionActId_value.setAttribute("value", jsonObject.getString("actionActId"));
									el_field_actionActId.appendChild(el_actionActId_value);

									el_handler.replaceChild(el_field_actionActId, node);
								} else if ("ownerValue".equals(el.getAttribute("name").trim())) {
									Element el_field_rightType = doc.createElement("field");
									el_field_rightType.setAttribute("name", "ownerValue");
									el_handler.appendChild(el_field_rightType);
									Element el_rightType_value = doc.createElement("string");
									el_rightType_value.setAttribute("value", jsonObject.getString("ownerValue"));
									el_field_rightType.appendChild(el_rightType_value);

									el_handler.replaceChild(el_field_rightType, node);
								} else if ("roleValue".equals(el.getAttribute("name").trim())) {
									Element el_field_rightValue = doc.createElement("field");
									el_field_rightValue.setAttribute("name", "roleValue");
									el_handler.appendChild(el_field_rightValue);
									Element el_rightValue_value = doc.createElement("string");
									el_rightValue_value.setAttribute("value", jsonObject.getString("roleValue"));
									el_field_rightValue.appendChild(el_rightValue_value);

									el_handler.replaceChild(el_field_rightValue, node);
								} else if ("sysEname".equals(el.getAttribute("name").trim())) {
									Element el_field_sysEname = doc.createElement("field");
									el_field_sysEname.setAttribute("name", "sysEname");
									el_handler.appendChild(el_field_sysEname);
									Element el_sysEname_value = doc.createElement("string");
									el_sysEname_value.setAttribute("value", jsonObject.getString("sysEname"));
									el_field_sysEname.appendChild(el_sysEname_value);

									el_handler.replaceChild(el_field_sysEname, node);
								} else if ("taskType".equals(el.getAttribute("name").trim())) {
									Element el_field_taskType = doc.createElement("field");
									el_field_taskType.setAttribute("name", "taskType");
									el_handler.appendChild(el_field_taskType);
									Element el_taskType_value = doc.createElement("string");
									el_taskType_value.setAttribute("value", jsonObject.getString("taskType"));
									el_field_taskType.appendChild(el_taskType_value);

									el_handler.replaceChild(el_field_taskType, node);
								} else if ("taskExeType".equals(el.getAttribute("name").trim())) {
									Element el_field_taskExeType = doc.createElement("field");
									el_field_taskExeType.setAttribute("name", "taskExeType");
									el_handler.appendChild(el_field_taskExeType);
									Element el_taskExeType_value = doc.createElement("string");
									el_taskExeType_value.setAttribute("value", jsonObject.getString("taskExeType"));
									el_field_taskExeType.appendChild(el_taskExeType_value);

									el_handler.replaceChild(el_field_taskExeType, node);
								} else if ("keepDays".equals(el.getAttribute("name").trim())) {
									Element el_field_keepDays = doc.createElement("field");
									el_field_keepDays.setAttribute("name", "keepDays");
									el_handler.appendChild(el_field_keepDays);
									Element el_keepDays_value = doc.createElement("string");
									el_keepDays_value.setAttribute("value", jsonObject.getString("keepDays"));
									el_field_keepDays.appendChild(el_keepDays_value);

									el_handler.replaceChild(el_field_keepDays, node);
								} else if ("crossDept".equals(el.getAttribute("name").trim())) {
									Element el_field_crossDept = doc.createElement("field");
									el_field_crossDept.setAttribute("name", "crossDept");
									el_handler.appendChild(el_field_crossDept);
									Element el_crossDept_value = doc.createElement("string");
									el_crossDept_value.setAttribute("value", jsonObject.getString("crossDept"));
									el_field_crossDept.appendChild(el_crossDept_value);

									el_handler.replaceChild(el_field_crossDept, node);
								}

							}

						}

						break;
					}

				}
				Transformer transformer = TransformerFactory.newInstance().newTransformer();
				transformer.setOutputProperty(OutputKeys.INDENT, "yes");
				StreamResult result = new StreamResult(new StringWriter());
				DOMSource source = new DOMSource(doc);
				transformer.transform(source, result);
				updatedProcessXml = result.getWriter().toString();
				this.showOk("节点信息修改成功");
			} catch (Exception e) {
				this.showError("节点信息修改失败");
				e.printStackTrace();
			} finally {
				flowInitManager.update(deploymentId, processName, updatedProcessXml);
			}
		} else {
			this.showError("提交信息有误");
		}

		return null;
	}

	/**
	 * 未发布时获取流程信息
	 * @return
	 * @throws Exception
	 * <p>@description 方法描述
	 * <p>@version 1.0
	 * <p>@author lvcai
	 * <p>@update 2013-7-18 下午05:39:35
	 */
	public String getFileInfo() throws Exception {
		/*FlowSys sys = flowSysManager.getFlowSysBySysEname(sysEname);
		String sys_url = null;
		if (null != sys) {
			sys_url = sys.getSysUrl();
		} else {
			this.showError("该系统不存在");
		}*/
		Map<String, Object> sysTempMap = new HashMap<String, Object>();
		nodeName = URLDecoder.decode(nodeName, "UTF-8");//nodeName = new String(nodeName.getBytes("ISO8859-1"), "GBK");
		String realPath = getFlowDir();

		JSONObject json = new JSONObject();

		try {
			InputStream inputStream = new FileInputStream(realPath + File.separator + uploadFileName);
			DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
			factory.setNamespaceAware(true);
			factory.setCoalescing(true);
			factory.setIgnoringComments(true);
			DocumentBuilder documentBuilder = null;
			documentBuilder = factory.newDocumentBuilder();
			Document doc = null;
			doc = documentBuilder.parse(inputStream);
			Element root = doc.getDocumentElement();
			NodeList nodes = root.getElementsByTagName("task");
			for (int i = 0; i < nodes.getLength(); i++) {
				Element ele = (Element) nodes.item(i);
				String name = ele.getAttribute("name").trim();
				if (name.equalsIgnoreCase(nodeName.trim())) {
					NodeList handler_node = ele.getElementsByTagName("assignment-handler");
					if (handler_node.getLength() > 0) {
						Element el_handler = (Element) handler_node.item(0);
						NodeList field_list = el_handler.getElementsByTagName("field");

						for (int j = 0; j < field_list.getLength(); j++) {
							Element el_field = (Element) field_list.item(j);
							Element el_string = (Element) el_field.getElementsByTagName("string").item(0);
							String key = el_field.getAttribute("name");
							if ("action".equals(key.trim()) || "actionActId".equals(key.trim())
									|| "sysEname".equals(key.trim())) {
								sysTempMap.put(key.trim(), el_string.getAttribute("value"));
							} else {
								json.put(key, el_string.getAttribute("value"));
							}
						}
						if (null != sysTempMap && sysTempMap.size() > 0) {//------
							String sys_url = null;
							ResultEntity resultEntity = UctCoreClient.getSysByProperty("sysEname",
									(String) sysTempMap.get("sysEname"));
							if (resultEntity == null) {
								this.logger.error("请求失败,通信异常!");
								this.showError("请求失败,通信异常!");
							} else {
								System.out.println("========响应=========Result:" + resultEntity.getResult()
										+ " ,Message : " + resultEntity.getMessage());
								if ("true".equals(resultEntity.getResult())) {
									Sys sysObj = (Sys) resultEntity.getEntity();
									sys_url = sysObj.getDelStatus();
								} else {
									this.logger.error("查询系统信息失败！" + resultEntity.getMessage());
									this.showError("查询系统信息失败！" + resultEntity.getMessage());
								}
							}
							String value = (String) sysTempMap.get("action");
							json.put("action", value.replace(sys_url, ""));
							json.put("actionActId", (String) sysTempMap.get("actionActId"));
							json.put("sysEname", (String) sysTempMap.get("sysEname"));
						} else {
							this.showError("定义中缺少必要的字段或字段值为空！");
						}
					}
					break;
				}
			}

			try {
				json.put("flag", "T");
				Struts2Utils.getResponse().getWriter().write(json.toString());
			} catch (IOException e) {
				e.printStackTrace();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}

		return null;
	}

	/**
	 * 发布后再修改流程时，获取流程初始化信息
	 * @return
	 * @throws Exception
	 * <p>@description 方法描述
	 * <p>@version 1.0
	 * <p>@author lvcai
	 * <p>@update 2013-7-18 下午05:38:48
	 */
	public String getXmlInfo() throws Exception {
		/*FlowSys sys = flowSysManager.getFlowSysBySysEname(sysEname);
		String sys_url = null;
		if (null != sys) {
			sys_url = sys.getSysUrl();
		} else {
			this.showError("该系统不存在");
		}*/
		Map<String, Object> sysTempMap = new HashMap<String, Object>();
		nodeName = URLDecoder.decode(nodeName, "UTF-8");//new String(nodeName.getBytes("ISO8859-1"), "GBK");
		RepositoryService repositoryService = processEngine.getRepositoryService();
		JSONObject json = new JSONObject();
		//获得该流程相关资源名称
		Set<String> resouceNames = repositoryService.getResourceNames(deploymentId);
		//流程图名称
		String processName = null;
		//TODO
		//目前认为取流程图片名称方法
		for (String name : resouceNames) {
			if (name.toLowerCase().endsWith(".xml")) {
				processName = name;
				break;
			}
		}
		//获得流程图流
		InputStream inputStream = repositoryService.getResourceAsStream(deploymentId, processName);

		String updatedProcessXml = "";
		try {
			DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
			factory.setNamespaceAware(true);
			factory.setCoalescing(true);
			factory.setIgnoringComments(true);
			DocumentBuilder documentBuilder = null;
			documentBuilder = factory.newDocumentBuilder();
			Document doc = null;
			doc = documentBuilder.parse(inputStream);
			Element root = doc.getDocumentElement();
			NodeList nodes = root.getElementsByTagName("task");
			for (int i = 0; i < nodes.getLength(); i++) {
				Element ele = (Element) nodes.item(i);
				String name = ele.getAttribute("name").trim();
				if (name.equalsIgnoreCase(nodeName.trim())) {
					NodeList handler_node = ele.getElementsByTagName("assignment-handler");
					if (handler_node.getLength() > 0) {
						Element el_handler = (Element) handler_node.item(0);
						NodeList field_list = el_handler.getElementsByTagName("field");

						for (int j = 0; j < field_list.getLength(); j++) {
							Element el_field = (Element) field_list.item(j);
							Element el_string = (Element) el_field.getElementsByTagName("string").item(0);
							String key = el_field.getAttribute("name");
							if ("action".equals(key.trim()) || "actionActId".equals(key.trim())
									|| "sysEname".equals(key.trim())) {
								sysTempMap.put(key.trim(), el_string.getAttribute("value"));
							} else {
								json.put(key, el_string.getAttribute("value"));
							}

						}

						if (null != sysTempMap && sysTempMap.size() > 0) {//------
							String sys_url = null;
							ResultEntity resultEntity = UctCoreClient.getSysByProperty("sysEname",
									(String) sysTempMap.get("sysEname"));
							if (resultEntity == null) {
								this.logger.error("请求失败,通信异常!");
								this.showError("请求失败,通信异常!");
							} else {
								System.out.println("========响应=========Result:" + resultEntity.getResult()
										+ " ,Message : " + resultEntity.getMessage());
								if ("true".equals(resultEntity.getResult())) {
									Sys sysObj = (Sys) resultEntity.getEntity();
									sys_url = sysObj.getDelStatus();
								} else {
									this.logger.error("查询系统信息失败！" + resultEntity.getMessage());
									this.showError("查询系统信息失败！" + resultEntity.getMessage());
								}
							}
							String value = (String) sysTempMap.get("action");
							json.put("action", value.replace(sys_url, ""));
							json.put("actionActId", (String) sysTempMap.get("actionActId"));
							json.put("sysEname", (String) sysTempMap.get("sysEname"));
						} else {
							this.showError("定义中缺少必要的字段或字段值为空！");
						}
					}
					break;

				}

			}

			try {
				json.put("flag", "T");
				Struts2Utils.getResponse().getWriter().write(json.toString());
			} catch (IOException e) {
				e.printStackTrace();
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			logger.info("deployFile : " + updatedProcessXml);
		}
		return null;
	}

	/**
	 * 
	 * @return
	 * <p>@description 获取流程定义文件上传存放路径
	 * <p>@version 1.0
	 * <p>@author lvcai
	 * <p>@update 2013-7-29 下午05:50:44
	 */
	private String getFlowDir() {
		ServletContext application = ServletActionContext.getRequest().getSession().getServletContext();
		ServletContextEvent event = new ServletContextEvent(application);
		String realPath = event.getServletContext().getRealPath("/flow/data");
		return realPath;
	}

	/**
	 * 
	 * @param from
	 * @param path
	 * @param fileName
	 * <p>@description 保存文件
	 * <p>@version 1.0
	 * <p>@author lvcai
	 * <p>@update 2013-7-29 下午05:51:09
	 */
	private void saveFile(File from, String path, String fileName) {
		try {
			FileInputStream in = new FileInputStream(from);
			OutputStream out = new FileOutputStream(path + File.separator + fileName);
			int b = 0;
			while ((b = in.read()) != -1) {
				out.write(b);
			}
			out.flush();
			out.close();
			in.close();
			System.out.println("文件写入完毕！");
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * 
	 * @return
	 * @throws IOException 
	 * @throws Exception
	 * <p>@description 导出工作流定义文件
	 * <p>@version 1.0
	 * <p>@author lvcai
	 * <p>@update 2013-7-19 上午09:06:49
	 */
	public String exportXml() throws IOException {
		RepositoryService repositoryService = processEngine.getRepositoryService();
		InputStream inputStream = repositoryService.getResourceAsStream(deploymentId, uploadFileName);

		HttpServletResponse response = Struts2Utils.getResponse();
		//1.设置文件ContentType类型，这样设置，会自动判断下载文件类型   
		response.setContentType("multipart/form-data");
		//2.设置文件头：最后一个参数是设置下载文件名
		response.setHeader("Content-Disposition", "attachment;fileName=" + uploadFileName);

		OutputStream output = response.getOutputStream();
		int len = 0;
		byte[] buffer = new byte[1024];
		while ((len = inputStream.read(buffer)) > 0) {
			output.write(buffer, 0, len);

		}
		inputStream.close();
		output.close();
		output.flush();

		return null;
	}

	public File getUpload() {
		return upload;
	}

	public void setUpload(File upload) {
		this.upload = upload;
	}

	public String getUploadFileName() {
		return uploadFileName;
	}

	public void setUploadFileName(String uploadFileName) {
		this.uploadFileName = uploadFileName;
	}

	public String getUploadContentType() {
		return uploadContentType;
	}

	public void setUploadContentType(String uploadContentType) {
		this.uploadContentType = uploadContentType;
	}

	public String getDefinitionId() {
		return definitionId;
	}

	public void setDefinitionId(String definitionId) {
		this.definitionId = definitionId;
	}

	public String getDeploymentId() {
		return deploymentId;
	}

	public void setDeploymentId(String deploymentId) {
		this.deploymentId = deploymentId;
	}

	public String getImgUploadFileName() {
		return imgUploadFileName;
	}

	public void setImgUploadFileName(String imgUploadFileName) {
		this.imgUploadFileName = imgUploadFileName;
	}

	public File getImgUpload() {
		return imgUpload;
	}

	public void setImgUpload(File imgUpload) {
		this.imgUpload = imgUpload;
	}

	public String getFormUrl() {
		return formUrl;
	}

	public void setFormUrl(String formUrl) {
		this.formUrl = formUrl;
	}

	public String getImgUploadContentType() {
		return imgUploadContentType;
	}

	public void setImgUploadContentType(String imgUploadContentType) {
		this.imgUploadContentType = imgUploadContentType;
	}

	public String getProcessInsId() {
		return processInsId;
	}

	public void setProcessInsId(String processInsId) {
		this.processInsId = processInsId;
	}

	public FlowInitManager getFlowInitManager() {
		return flowInitManager;
	}

	public void setFlowInitManager(FlowInitManager flowInitManager) {
		this.flowInitManager = flowInitManager;
	}

	public String getJsonnodes() {
		return jsonnodes;
	}

	public void setJsonnodes(String jsonnodes) {
		this.jsonnodes = jsonnodes;
	}

	public String getTaskId() {
		return taskId;
	}

	public void setTaskId(String taskId) {
		this.taskId = taskId;
	}

	public String getRows() {
		return rows;
	}

	public void setRows(String rows) {
		this.rows = rows;
	}

	public String getPage() {
		return page;
	}

	public void setPage(String page) {
		this.page = page;
	}

	/*public List<FlowSys> getSysList() {
		return sysList;
	}

	public void setSysList(List<FlowSys> sysList) {
		this.sysList = sysList;
	}*/

	public String getSysEname() {
		return sysEname;
	}

	public void setSysEname(String sysEname) {
		this.sysEname = sysEname;
	}

	public List<FlowTaskUrl> getUrlList() {
		return urlList;
	}

	public void setUrlList(List<FlowTaskUrl> urlList) {
		this.urlList = urlList;
	}

	public FlowDeploy getFlowDeploy() {
		return flowDeploy;
	}

	public void setFlowDeploy(FlowDeploy flowDeploy) {
		this.flowDeploy = flowDeploy;
	}

	public String getSysId() {
		return sysId;
	}

	public void setSysId(String sysId) {
		this.sysId = sysId;
	}

	public String getNodeName() {
		return nodeName;
	}

	public void setNodeName(String nodeName) {
		this.nodeName = nodeName;
	}

	public String getNodeId() {
		return nodeId;
	}

	public void setNodeId(String nodeId) {
		this.nodeId = nodeId;
	}

	public String getDeployId() {
		return deployId;
	}

	public void setDeployId(String deployId) {
		this.deployId = deployId;
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public String getStatus() {
		return status;
	}

	public void setStatus(String status) {
		this.status = status;
	}

	public List<Org> getOrgList() {
		return orgList;
	}

	public void setOrgList(List<Org> orgList) {
		this.orgList = orgList;
	}

	public String getOrgId() {
		return orgId;
	}

	public void setOrgId(String orgId) {
		this.orgId = orgId;
	}

	public String getOrgCname() {
		return orgCname;
	}

	public void setOrgCname(String orgCname) {
		this.orgCname = orgCname;
	}

	public String getOperType() {
		return operType;
	}

	public void setOperType(String operType) {
		this.operType = operType;
	}

	public String getSysCname() {
		return sysCname;
	}

	public void setSysCname(String sysCname) {
		this.sysCname = sysCname;
	}

}
